package com.wudizaba.pg.typeHandler;

import cn.hutool.json.JSONUtil;
import com.wudizaba.pg.domain.po.veryGoodFriends;
import org.apache.ibatis.type.JdbcType;
import org.apache.ibatis.type.MappedJdbcTypes;
import org.apache.ibatis.type.MappedTypes;
import org.apache.ibatis.type.TypeHandler;

import java.sql.CallableStatement;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

@MappedJdbcTypes(JdbcType.VARCHAR) // 指定对应的JDBC类型
@MappedTypes(veryGoodFriends.class) // 指定对应的Java类型
public class MyCustomTypeHandler implements TypeHandler<veryGoodFriends> {


    @Override
    public void setParameter(PreparedStatement preparedStatement, int i, veryGoodFriends veryGoodFriends, JdbcType jdbcType) throws SQLException {
        String s = veryGoodFriends.toString();
        preparedStatement.setString(i, s);
    }

    @Override
    public veryGoodFriends getResult(ResultSet resultSet, String s) throws SQLException {
        String string = resultSet.getString(s);
        return JSONUtil.toBean(string, veryGoodFriends.class);
    }

    @Override
    public veryGoodFriends getResult(ResultSet resultSet, int i) throws SQLException {
        String string = resultSet.getString(i);
        return JSONUtil.toBean(string, veryGoodFriends.class);
    }

    @Override
    public veryGoodFriends getResult(CallableStatement callableStatement, int i) throws SQLException {
        String string = callableStatement.getString(i);
        return JSONUtil.toBean(string, veryGoodFriends.class);
    }
}
